package com.punai.monthanalysis.bo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.common.utils.bean.BeanUtils;
import com.punai.externalccb.bo.YdCbfeeiteminfoStandardBo;
import com.punai.monthanalysis.domain.ProYdcbfxDwgcrgfzldtkzb;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

public class ProYdcbfxDwgcrgfzldtkzbBo extends ProYdcbfxDwgcrgfzldtkzb {
    private Integer rowspan = 1;
    private Integer colspan = 1;
    private List<ProYdcbfxDwgcrgfzldtkzbBo> children = new ArrayList<>();

    public Integer getRowspan() {
        return rowspan;
    }

    public void setRowspan(Integer rowspan) {
        this.rowspan = rowspan;
    }

    public Integer getColspan() {
        return colspan;
    }

    public void setColspan(Integer colspan) {
        this.colspan = colspan;
    }

    public List<ProYdcbfxDwgcrgfzldtkzbBo> getChildren() {
        return children;
    }

    public void setChildren(List<ProYdcbfxDwgcrgfzldtkzbBo> children) {
        this.children = children;
    }

    public void clearColRow() {
        this.setRowspan(0);
        this.setColspan(0);
    }

    public static List<ProYdcbfxDwgcrgfzldtkzbBo> convert(List<ProYdcbfxDwgcrgfzldtkzb> inList) {
        List<ProYdcbfxDwgcrgfzldtkzbBo> result = new ArrayList<>();
        if (CollUtil.isEmpty(inList)) {
            return result;
        }

        ListValueMap<String,ProYdcbfxDwgcrgfzldtkzb> levelMap = new ListValueMap<>();
        for (ProYdcbfxDwgcrgfzldtkzb gcjdkhcldbfxb : inList) {
            levelMap.putValue(StrUtil.blankToDefault(gcjdkhcldbfxb.getPid(),"0"),gcjdkhcldbfxb);
        }
        List<ProYdcbfxDwgcrgfzldtkzb> roots = levelMap.get("0");
        roots.sort(Comparator.comparing(ProYdcbfxDwgcrgfzldtkzb::getXh));


        // 只拆解两层
        for (ProYdcbfxDwgcrgfzldtkzb root : roots) {
            String id = root.getId();
            ProYdcbfxDwgcrgfzldtkzbBo convert = convert(root);
            List<ProYdcbfxDwgcrgfzldtkzb> children = levelMap.get(id);
            if (CollUtil.isNotEmpty(children)) {
                convert.setRowspan(children.size() + 1);
                result.add(convert);
                children.sort(Comparator.comparing(ProYdcbfxDwgcrgfzldtkzb::getXh));
                for (ProYdcbfxDwgcrgfzldtkzb child : children) {
                    ProYdcbfxDwgcrgfzldtkzbBo childrenBo = convert(child);
                    childrenBo.clearColRow();
                    result.add(childrenBo);
                }
            } else {
                result.add(convert);
            }
        }
        return result;
    }

    public static ProYdcbfxDwgcrgfzldtkzbBo convert(ProYdcbfxDwgcrgfzldtkzb in) {
        ProYdcbfxDwgcrgfzldtkzbBo out = new ProYdcbfxDwgcrgfzldtkzbBo();
        BeanUtils.copyBeanProp(out,in);
        return out;
    }

}
